/*
 *  Copyright 2013 Utkin Dmitry
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#ifndef PROTOQTRPC_SOCKETRPCSERVERSESSION_H
#define PROTOQTRPC_SOCKETRPCSERVERSESSION_H

#include <string>
#include <QObject>
#include <QThread>
#include <QAbstractSocket>
#include "RpcServerSession.h"

class QTcpSocket;

namespace protoqtrpc
{
class SocketRpcServer;
class SocketRpcProtocol;

/**
 * @brief Socket peer information
 */
struct SocketPeerInfo: public PeerInfo
{
    quint32 address;  //!< IP-address of the client
    quint16 port;     //!< port of the client
};

/**
 * @brief Socket implementation of server session
 */
class SocketRpcServerSession: public RpcServerSession
{
    Q_OBJECT

public:
    /**
     * @brief Constructs the SocketRpcServerSession and initializes the session
     * @param sock Socket to client
     * @param server RPC Server
     */
    SocketRpcServerSession(QTcpSocket* sock, SocketRpcServer* server);

    /**
     * @brief Destroys the SocketRpcServerSession and frees connection
     */
    virtual ~SocketRpcServerSession();

protected:
    /**
     * @brief Gets peer info
     * @return peer info
     */
    virtual const PeerInfo& peerInfo();

protected slots:
    void socketError(QAbstractSocket::SocketError error);

private:
    QTcpSocket* mSock;
    SocketRpcProtocol* mRpcProtocol;
    QThread mThread;
    SocketPeerInfo mPeerInfo;
};

}

#endif
